//luogu.P1886
#include <bits/stdc++.h>

#define N 10000000
int a[N];
std::deque<int> q;

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++)
    {
        while(!q.empty() && a[q.back()] > a[i]) q.pop_back();
        q.push_back(i);
        if(i >= k)
        {
            while(!q.empty() && q.front() <= i-k)q.pop_front();
            printf("%d ", a[q.front()]);
        }
    }
    printf("\n");
    q.clear();
    for(int i = 1; i<= n; i++)
    {
        while(!q.empty() && a[q.back()] < a[i]) q.pop_back();
        q.push_back(i);
        if(i >= k)
        {
            while(!q.empty() && q.front() <= i-k) q.pop_front();
            printf("%d ", a[q.front()]);
        }
    }
    printf("\n");
    return 0;
}